package com.ym.soso.service.search.service;

import com.ym.base.domain.MobileSearchParam;
import com.ym.common.utils.StringUtil;
import com.ym.core.es.query.QueryParser;
import com.ym.core.es.service.AbstractHeightWordOperationService;
import com.ym.soso.domain.entity.PensionInfoEntity;
import com.ym.soso.enums.PublishStatusEnum;
import com.ym.soso.service.IPensionInfoSV;
import com.ym.soso.service.search.index.PensionInfoIndex;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 养老
 *
 * @author: 李涛
 * @version: 2020年01月07日 14:17
 */
@Service
@Slf4j
public class PensionInfoIndexService extends AbstractHeightWordOperationService<PensionInfoIndex, PensionInfoEntity> {

	@Autowired
	private IPensionInfoSV pensionInfoSV;

	@Override
	public List<PensionInfoEntity> getData() {
		List<PensionInfoEntity> initData = pensionInfoSV.selectAll();
		return initData;
	}

	@Override
	public PensionInfoIndex beanCovert(PensionInfoEntity pensionInfo) {
		PensionInfoIndex index = new PensionInfoIndex();
		index.setId(pensionInfo.getId().toString());
		index.setTags(pensionInfo.getTags());
		index.setTheName(pensionInfo.getTheName());
		index.setProv(pensionInfo.getProv());
		index.setCity(pensionInfo.getCity());
		index.setArea(pensionInfo.getArea());
		index.setAdmittedType(pensionInfo.getAdmittedType());
		index.setPublishStatus(pensionInfo.getPublishStatus());
		return index;
	}


	@Override
	protected QueryParser buildSearchIdsConditions(MobileSearchParam args)  {
		String prov = args.getProv();
		String city = args.getCity();
		String area = args.getArea();
		String keyword = args.getKeyword();
		// 构建查询语句
		QueryParser queryParser = new QueryParser(dataTypeClass);
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
		// 省市区
		if (StringUtil.isNotBlank(prov)) {
			boolQueryBuilder.filter(QueryBuilders.termQuery("prov", prov));
		}
		if (StringUtil.isNotBlank(city)) {
			boolQueryBuilder.filter(QueryBuilders.termQuery("city", city));
		}
		if (StringUtil.isNotBlank(area)) {
			boolQueryBuilder.filter(QueryBuilders.termQuery("area", area));
		}
		boolQueryBuilder.filter(QueryBuilders.termQuery("publishStatus", PublishStatusEnum.HAS_BEEN_RELEASED.getKey()));
		MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyword, "tags", "admittedType", "theName");
		multiMatchQueryBuilder.minimumShouldMatch("25%");
		boolQueryBuilder.must(multiMatchQueryBuilder);
		queryParser.query(boolQueryBuilder,new String[]{"id"});
		return queryParser;
	}

}
